﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using daSyntaxAnalyser.SyntaxTree;
using CQLQueries.Analyzers;
using System.Text.RegularExpressions;

namespace CQLQueries.SyntaxTree
{
    public class Relation4Node
    {
        public object NodeTree;
        public object ValueUp;

        public RelationNode Relation;
        public ExpressionNode Expression1;
        public ExpressionNode Expression2;
        public CQLTokens RelOp;

        public Relation4Node()
        {            
        }

        public void Visit()
        {
            if (Relation != null)
            {
                Relation.NodeTree = NodeTree;
                Relation.Visit();
                ValueUp = Relation.ValueUp;
            }
            else
            {
                Expression1.NodeTree = NodeTree;
                Expression1.Visit();
                Expression2.NodeTree = NodeTree;
                Expression2.Visit();
                switch (RelOp)
                { 
                    case CQLTokens.Less:
                        ValueUp = Convert.ToDouble(Expression1.ValueUp) < Convert.ToDouble(Expression2.ValueUp);
                        break;
                    case CQLTokens.LessOrEqual:
                        ValueUp = Convert.ToDouble(Expression1.ValueUp) <= Convert.ToDouble(Expression2.ValueUp);
                        break;
                    case CQLTokens.More:
                        ValueUp = Convert.ToDouble(Expression1.ValueUp) > Convert.ToDouble(Expression2.ValueUp);
                        break;
                    case CQLTokens.MoreOrEqual:
                        ValueUp = Convert.ToDouble(Expression1.ValueUp) >= Convert.ToDouble(Expression2.ValueUp);
                        break;
                    case CQLTokens.Equal:
                        try
                        {
                            ValueUp = Convert.ToDouble(Expression1.ValueUp) == Convert.ToDouble(Expression2.ValueUp);
                        }
                        catch
                        {
                            ValueUp = Convert.ToString(Expression1.ValueUp) == Regex.Replace(Convert.ToString(Expression2.ValueUp), "\"", "");
                        }                       
                        break;
                    case CQLTokens.NotEqual:
                        if (Expression1.ValueUp.GetType().Name == "Double")
                        {
                            ValueUp = Convert.ToDouble(Expression1.ValueUp) != Convert.ToDouble(Expression2.ValueUp);
                        }
                        else if (Expression1.ValueUp.GetType().Name == "String")
                        {
                            ValueUp = Convert.ToString(Expression1.ValueUp) != Regex.Replace(Convert.ToString(Expression2.ValueUp), "\"", "");
                        } 
                        break;
                    case CQLTokens.Like:
                        String str2 = Convert.ToString(Expression2.ValueUp);
                        str2 = Regex.Replace(str2, "\"", "");
                        ValueUp = Regex.IsMatch(Convert.ToString(Expression1.ValueUp), str2);
                        break;
                    case CQLTokens.Not:
                        break;
                }
            }
        }
    }
}
